<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="名称"></el-input>
      </el-form-item>
      <el-form-item label="种类" prop="category">
        <el-input v-model="dataForm.category" placeholder="种类"></el-input>
      </el-form-item>
      <el-form-item label="内容" prop="context">
        <div class="content">
          <vue-html5-editor
            :content="content"
            :height="400"
            @change="updateData"
          ></vue-html5-editor>
        </div>
        <!--<el-input v-model="dataForm.context" placeholder="内容"></el-input>-->
      </el-form-item>
      <el-form-item label="时间" prop="date">
        <el-input v-model="dataForm.date" placeholder="时间"></el-input>
      </el-form-item>
      <el-form-item label="发布单位" prop="publishCompany">
        <el-input
          v-model="dataForm.publishCompany"
          placeholder="发布单位"
        ></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      content:'请输入并编辑内容',
      dataForm: {
        id: 0,
        name: "",
        category: "",
        context: "",
        date: "",
        publishCompany: ""
      },
      dataRule: {
        name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
        category: [
          { required: true, message: "类型不能为空", trigger: "blur" }
        ],
        date: [{ required: true, message: "时间不能为空", trigger: "blur" }],
        publishCompany: [
          { required: true, message: "发布单位不能为空", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    updateData(e) {
      console.log(e)
      this.dataForm.context = e;
    },
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/huinong/policiesRegulations/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.id = data.policiesRegulationsEntity.id;
              this.dataForm.name = data.policiesRegulationsEntity.name;
              this.dataForm.category = data.policiesRegulationsEntity.category;
              this.content= data.policiesRegulationsEntity.context;
              this.dataForm.date = data.policiesRegulationsEntity.date;
              this.dataForm.publishCompany =
                data.policiesRegulationsEntity.publishCompany;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/huinong/policiesRegulations/${
                !this.dataForm.id ? "save" : "update"
              }`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              name: this.dataForm.name,
              category: this.dataForm.category,
              context: this.dataForm.context,
              date: this.dataForm.date,
              publishCompany: this.dataForm.publishCompany
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",

                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
